package com.example.cardgame

import android.content.Intent
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class FirstActivity : AppCompatActivity() {

    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var nameTextView: TextView
    private lateinit var genderTextView: TextView
    private lateinit var studentIdTextView: TextView
    private lateinit var phoneTextView: TextView
    private lateinit var addressTextView: TextView
    private lateinit var hobbiesTextView: TextView
    private lateinit var highScoreTextView: TextView
    private lateinit var imageView: ImageView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)

        sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)

        nameTextView = findViewById(R.id.nameTextView)
        genderTextView = findViewById(R.id.genderTextView)
        studentIdTextView = findViewById(R.id.studentIdTextView)
        phoneTextView = findViewById(R.id.phoneTextView)
        addressTextView = findViewById(R.id.addressTextView)
        hobbiesTextView = findViewById(R.id.hobbiesTextView)
        highScoreTextView = findViewById(R.id.highScoreTextView)
        imageView = findViewById(R.id.imageView)

        loadUserInfo()

        findViewById<Button>(R.id.modifyButton).setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, 1)
        }

        findViewById<Button>(R.id.gameButton).setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }

    private fun loadUserInfo() {
        nameTextView.text = "姓名：" + sharedPreferences.getString("name", "N/A")
        genderTextView.text = "性别：" + sharedPreferences.getString("gender", "N/A")
        studentIdTextView.text = "学号：" + sharedPreferences.getString("studentId", "N/A")
        phoneTextView.text = "电话：" + sharedPreferences.getString("phone", "N/A")
        addressTextView.text = "邮箱地址：" + sharedPreferences.getString("address", "N/A")
        hobbiesTextView.text = "爱好：" + sharedPreferences.getString("hobbies", "N/A")
        highScoreTextView.text = "历史最高分：" + sharedPreferences.getInt("highScore", 0).toString()

        val imagePath = sharedPreferences.getString("profile_image_path", null)
        if (imagePath != null) {
            val bitmap = BitmapFactory.decodeFile(imagePath)
            imageView.setImageBitmap(bitmap)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == RESULT_OK) {
            loadUserInfo()
        }
    }
}